import { getConfig, setConfig } from "@/services/infraRed";
import { makeAutoObservable, action, toJS } from "mobx";


class InfraRedConfig {
  constructor() {
    makeAutoObservable(this);
  }
  data = {}

  @action.bound
  fetchData = async (mac: string, productKey: string) => {
    const data = await getConfig(mac, productKey);
    if (data.success) {
      this.data[mac] = [];
      try {
        this.data[mac] = JSON.parse(data.data as any || '[]')
      } catch (error) {
        console.error('error', error)
      }
      
      this.data = {...toJS(this.data)};
    }
  }
  @action.bound
  saveData = (mac: string, productKey: string, config: any) => {
    this.data[mac] = config;
    setConfig(mac, productKey, config);
    this.data = {...this.data};

  }
}

const InfraRedConfig_Store_Key = "infraRedConfig";
export { InfraRedConfig, InfraRedConfig_Store_Key };

export default new InfraRedConfig();
